<?php
namespace app\admin\model;
use think\Model;

class AuthRule extends Model
{
    
   public function authTree(){
    	$authres=$this->order('sort asc')->select();
    	return $this->sort($authres);
    }

    public function sort($data,$pid=0){
    	static $arr=array();
    	foreach ($data as $k=> $v) {
    		if($v['pid']==$pid){ 
                $v['dataid']=$this->getparentid($v['id']);     
    			$arr[]=$v;
    			$this->sort($data,$v['id']);
    		}
    	}
    	return $arr;
    }

    public function getchildren($authid){
    	$authres=$this->select();
    	return $this->_getchildren($authres,$authid);
    }

    public function _getchildren($authres,$authid){
    	static $arr=array();
    	
    	foreach ($authres as $k => $v) {

    		if($v['pid']== $authid){

    			$arr[]=$v['id'];
    			$this->_getchildren($authres,$v['id']);
    		}
    	}
    	return $arr;
    }

    //获得当前修改栏目的家谱树
    public function getTree($id){
    		static $tree=array();
    		$auths=$this->select();

    	while ($id>0){
    		foreach ($auths as $v) {
    			if($v['id']== $id){
    				$tree[]=$v;

    				$id=$v['pid'];
    				break;
    			}
    		}
    	}
    	return $tree;
    }

    public function getparentid($authid){

        $authres=$this->select();  
        return $this->_getparentid($authres,$authid,true);
    }

    public function _getparentid($authres,$authid,$clear=false){
        static $arr=array();
        if($clear){
            $arr=array();
        }
        foreach ($authres as $k => $v) {

            if($v['id']== $authid){
                $arr[]=$v['id'];
                $this->_getparentid($authres,$v['pid'],false);
            }
        }
        asort($arr);
        //dump($arr);die;
        $arrstr=implode('-', $arr);
        //dump($arrstr);die;
        return $arrstr;
    }
}
